﻿using CgdataBase;
using FolderCompare.Helpers;
using FolderCompare.Models;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FolderCompare
{
    public static class AppBase
    {
        public static void DeleteItems(CgFileSystemInfo[] items, CgDirectoryInfo currentDir, CgDirectoryInfo otherDir)
        {
            foreach (var item in items)
            {
                if (item is CgFileInfo file)
                {
                    File.Delete(file.FullName);
                }
                else if (item is CgDirectoryInfo dir)
                {
                    Directory.Delete(dir.FullName, true);
                }

                var info2 = otherDir.Children.Single(s => s.Index == item.Index);

                if (info2.Name.IsNullOrEmpty())
                {
                    otherDir.Children.Remove(info2);
                    currentDir.Children.Remove(item);
                }
                else
                {
                    var index = currentDir.Children.IndexOf(item);
                    currentDir.Children.RemoveAt(index);

                    if (info2 is CgFileInfo fileInfo)
                    {
                        currentDir.Children.Insert(index, new CgFileInfo { Index = info2.Index, Result = ECompareResult.空 });
                        fileInfo.Result = ECompareResult.孤立;
                    }
                    else if (info2 is CgDirectoryInfo directoryInfo)
                    {
                        currentDir.Children.Insert(index, new CgDirectoryInfo { Index = info2.Index, Result = ECompareResult.空 });
                        directoryInfo.Result = ECompareResult.孤立;
                        CompareHelper.SetCompareResult(directoryInfo, ECompareResult.匹配);
                    }
                }
            }

            currentDir.Result = CompareHelper.GetCompareResult(currentDir.Children);
            otherDir.Result = CompareHelper.GetCompareResult(otherDir.Children);
        }
    }
}